define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'business/privatesea/index',
                    add_url: 'business/privatesea/add',
                    edit_url: 'business/privatesea/edit',
                    del_url: 'business/privatesea/del',
                    table: 'business',
                },
                pageSize: 20, //调整分页大小为20
                pageList: [10, 25, 50, 100, 'All'], //增加一个100的分页大小
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                // 当访问列表发起ajax请求数据
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                // 主键
                pk: 'id',
                // 设置默认排序
                sortName: 'id',
                sortOrder: 'desc',
                search: false, // 取消快速搜索框
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'email', title: __('Email'), operate: 'LIKE' },
                        { field: 'money', title: __('Money'), operate: '>=',table:table,class:'autocontent',formatter: function(value){
                            if(!value){
                                return '0.00';
                            }
                            return value;
                        }},
                        { field: 'scource.name', title: __('SourceName'), operate: 'LIKE',searchList:$.getJSON("business/privatesea/source") },
                        { field: 'gender', title: __('Gender'),formatter: Table.api.formatter.normal, searchList: { "0": __('保密'), "1": __('男'), "2": __('女') }},
                        { field: 'deal', title: __('Deal'),formatter: Table.api.formatter.normal, searchList: { "0": __('未成交'), "1": __('已成交') } },
                        { field: 'auth', title: __('Auth'), searchList: { "0": __('未认证'), "1": __('已认证') },formatter: Table.api.formatter.normal },
                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                        buttons: [
                            {
                                name: 'business',
                                title: '客户详情',
                                extend: 'data-toggle="tooltip"',
                                classname: "btn btn-xs btn-primary btn-dialog vivst_test",
                                icon: 'fa fa-eye',
                                url: 'business/privateseainfo/index?ids={id}',
                            },
                            {
                                name: 'recovery',  confirm: '确定要回收吗', title: '客户回收',
                                extend: 'data-toggle="tooltip"',
                                icon: 'fa fa-recycle',
                                classname: 'btn btn-xs btn-success btn-ajax',
                                url: 'business/privatesea/recovery?ids={id}',
                                success: function (data, ret) {
                                    $(".btn-refresh").trigger("click");
                                },
                                error: function (err) {
                                    console.log(err);
                                }
                            }

                        ]
                    }
                    ]
                ]
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        // 添加
        add: function () {
            $("#c-city").on("cp:updated", function() {
                var citypicker = $(this).data("citypicker");
                var code = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province");
                $("#code").val(code);
              });
            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-city").on("cp:updated", function() {
                var citypicker = $(this).data("citypicker");
                var code = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province");
                $("#code").val(code);
              });
            Controller.api.bindevent();
        },
        del: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    }

    return Controller;
});